package a66weding.com.jiehuntong.application;


import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;

import java.io.File;

import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.beans.VersionBean;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.http.RequestCode;


/**
 * 数据仓库，用于操作全局变量里面的数据
 *
 * @author Administrator
 */
public class DataWareHourse {

    private static DataWareHourse dwh = new DataWareHourse();

    private DataWareHourse() {

    }

    public static DataWareHourse getInstance() {
        return dwh;
    }

    public User mUser;

    public File saveDir;

    public VersionBean versionBean;

    public static Context mContext;

    /**
     * 初始化一个 Application 供全局使用
     *
     * @param context
     * @return
     */
    public static GlobalApplication getGlobApplication(Context context) {
        mContext = context;
        return (GlobalApplication) context.getApplicationContext();
    }


    /**
     * 获取应用版本号
     *
     * @return 应用版本号字符串, 如果不存在返回 null
     */
    public static String getAppVersion(Context context) {
        PackageManager manager = context.getPackageManager();
        try {
            return manager.getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void initFiles() {
        Environment.getExternalStorageDirectory();
        String storageState = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(storageState)) {
            saveDir = mContext.getExternalFilesDir(null);

        } else {
            saveDir = mContext.getFilesDir();

        }
    }


    public static DataWareHourse getDwh() {
        return dwh;
    }

    public static void setDwh(DataWareHourse dwh) {
        DataWareHourse.dwh = dwh;
    }

    public File getSaveDir(Context context) {
        mContext = context;
        initFiles();
        return saveDir;
    }

    public User getmUser() {
        if (mUser == null) {
            mUser = CacheUtils.readObjectFromTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, User.class);
        }

        return mUser;
    }

    public void setmUser(User mUser) {
        this.mUser = mUser;
    }

    public VersionBean getVersionBean() {
        return versionBean;
    }

    public void setVersionBean(VersionBean versionBean) {
        this.versionBean = versionBean;
    }
}
